package io.renren.modules.security.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.exception.RenException;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.modules.security.dao.KeywordDao;
import io.renren.modules.security.dao.KeywordTypeDao;
import io.renren.modules.security.dto.SelectKeywordDto;
import io.renren.modules.security.dto.UpdateKeywordDto;
import io.renren.modules.security.entity.Keyword;
import io.renren.modules.security.entity.KeywordType;
import io.renren.modules.security.service.KeywordService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class KeywordServiceImpl extends BaseServiceImpl<KeywordDao, Keyword> implements KeywordService {

    @Resource
    private KeywordTypeDao keywordTypeDao;

    public Page<Keyword> getPage(SelectKeywordDto dto){
        LambdaQueryWrapper<Keyword> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null != dto.getKeywordType(), Keyword::getKeywordTypeId, dto.getKeywordType());
        wrapper.like(StrUtil.isNotBlank(dto.getKeyword()), Keyword::getWordName, dto.getKeyword());
        wrapper.orderByAsc(Keyword::getKeywordTypeId);
        wrapper.orderByDesc(Keyword::getCreateTime);
        Page<Keyword> result = this.baseDao.selectPage(Page.of(dto.getCurrentPage(), dto.getPageSize()), wrapper);
        if (CollUtil.isNotEmpty(result.getRecords())) {
            List<KeywordType> typeList = keywordTypeDao.selectList(new LambdaQueryWrapper<>());
            Map<Long, KeywordType> map = typeList.stream().collect(Collectors.toMap(KeywordType::getId, k -> k));
            result.getRecords().forEach(c -> c.setKeywordType(map.get(c.getKeywordTypeId()).getKeywordType()));
        }
        return result;
    }

    public boolean add(UpdateKeywordDto dto){
        verify(dto);
        Keyword keyword = new Keyword();
        BeanUtil.copyProperties(dto, keyword);
        return this.baseDao.insert(keyword) > 0;
    }

    public boolean update(UpdateKeywordDto dto){
        verify(dto);
        Keyword keyword = this.baseDao.selectById(dto.getId());
        if(null == keyword){
            throw new RenException("该关键词不存在");
        }
        BeanUtil.copyProperties(dto, keyword);
        return this.baseDao.updateById(keyword) > 0;
    }

    public boolean delete(Long id){
        Keyword keyword = this.baseDao.selectById(id);
        if(null == keyword){
            throw new RenException("该关键词不存在");
        }
        return this.baseDao.deleteById(id) > 0;
    }

    private void verify(UpdateKeywordDto dto) {
        LambdaQueryWrapper<Keyword> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Keyword::getKeywordTypeId, dto.getKeywordTypeId());
        wrapper.eq(Keyword::getWordName, dto.getWordName());
        wrapper.eq(Keyword::getWordEn, dto.getWordEn());
        wrapper.ne(Keyword::getId, dto.getId());
        if (this.baseDao.selectCount(wrapper) > 0) {
            throw new RenException("该关键词已存在");
        }
    }
}
